/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

import (
	"barge/cmd"
	_ "barge/init"
	"barge/pkg/util"
	"github.com/rs/zerolog/log"
	"github.com/spf13/viper"
	"os"
)

func initFile() error {
	if !util.IsFileExists(viper.GetString("sys.mainDir")) {
		err := os.MkdirAll(viper.GetString("sys.mainDir"), os.ModePerm)
		if err != nil {
			log.Err(err).Msg("create main dir failed")
			return err
		}
	}
	if !util.IsFileExists(viper.GetString("sys.aesPath")) {
		str := util.RandomAlphanumericString(32)
		f, err := os.Create(viper.GetString("sys.aesPath"))
		if err != nil {
			return err
		}
		defer f.Close()
		_, err = f.WriteString(str)
		log.Info().Msg("generate aes key success")
	}
	return nil
}

func main() {
	err := initFile()
	if err != nil {
		return
	}
	aesKey, err := os.ReadFile(viper.GetString("sys.aesPath"))
	if err != nil {
		return
	}
	viper.Set("sys.aesKey", aesKey)
	cmd.Execute()
}
